<template>
  <div class="swiper-container" :ref="someRef">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in someList" :key="item.id">
        <img :src="item.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
export default {
  name: 'Carousel',
  props: ['someList', 'someRef'],
  watch: {
    someList: {
      handler() {
        this.$nextTick(() => {
          new Swiper(this.$refs[this.someRef], {
            spaceBetween: 30, //控制每一屏中间的间隔
            speed: 1000, //切换时间
            loop: true, //无缝轮播
            // centeredSlides: true,
            autoplay: {
              // 自动轮播
              delay: 2000, //延时时间 当前屏显示多久 去下一张
              disableOnInteraction: false, //鼠标操作之后 是否禁止自动轮播
            },
            pagination: {
              //小圆点
              el: '.swiper-pagination', //选择到那个小圆点元素
              clickable: true, //是否可以点击小圆点
            },
            navigation: {
              // 左右按钮
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
          })
        })
      },
      immediate: true,
    },
  },
}
</script>

<style lang="less" scoped></style>
